﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

//https://go.microsoft.com/fwlink/?LinkId=234236 上介绍了“用户控件”项模板

namespace XiaoweiUWP.HpControls
{

    public enum Status
    {
        UnSelect,
        Selected

    }
    public class HpStatesBtnViewModel : INotifyPropertyChanged
    {

        private CoreDispatcher Dispatcher;
        public HpStatesBtnViewModel(CoreDispatcher d)
        {
            this.Dispatcher = d;

        }
        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanegd(string propertyName)
        {
            if (PropertyChanged != null)
            {
                _ = this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                });

            }
        }
        private string url = "";
        public string ImgUrl
        {
            set
            {
                if (url != value)
                {
                    url = value;
                    OnPropertyChanegd(nameof(ImgUrl));
                }
            }
            get
            {
                return url;
            }
        }
        public string SelectIcon
        {
            set;
            get;
        }
        public string UnSelectIcon
        {
            set;
            get;
        }
        private Status mstatus;
        public Status SStatus
        {
            set
            {
                mstatus = value;
                if (mstatus == Status.Selected)
                {
                    if (!string.IsNullOrEmpty(SelectIcon))
                    {
                        ImgUrl = SelectIcon;
                    }
                }
                else
                {
                    if (!string.IsNullOrEmpty(UnSelectIcon))
                    {
                        ImgUrl = UnSelectIcon;
                    }
                }
            }
            get
            {
                return mstatus;
            }

        }
    }

    public sealed partial class HpStatesBtn : UserControl
    {
        private HpStatesBtnViewModel vm;
        public HpStatesBtn()
        {
            this.InitializeComponent();
            this.vm = new HpStatesBtnViewModel(this.Dispatcher);
            this.DataContext = this.vm;
            this.Loaded += HpStatesBtn_Loaded;
        }
        public event Action Click;
        private void HpStatesBtn_Loaded(object sender, RoutedEventArgs e)
        {
            this.vm.UnSelectIcon = UnSelectIcon;
            this.vm.SelectIcon = SelectIcon;
            this.SStatus = Status.UnSelect;
            this.vm.SStatus = Status.UnSelect;


        }

        public string SelectIcon
        {
            set;
            get;
        }
        public string UnSelectIcon
        {
            set;
            get;
        }

        public Status SStatus
        {
            set;
            get;
        }
        private void Grid_PointerPressed(object sender, PointerRoutedEventArgs e)
        {
            if (SStatus == Status.UnSelect)
            {
                SStatus = Status.Selected;
                this.bLine.Visibility = Visibility.Visible;
                this.vm.SStatus = SStatus;
            }     
            Click?.Invoke();
        }
        public void SetSelected()
        {
            if (SStatus == Status.UnSelect)
            {
                SStatus = Status.Selected;
                this.bLine.Visibility = Visibility.Visible;
                this.vm.SStatus = SStatus;
            }
        }
        public void SetUnSelected()
        {
            if (SStatus == Status.Selected)
            {
                SStatus = Status.UnSelect;
                this.bLine.Visibility = Visibility.Collapsed;
                this.vm.SStatus = SStatus;
            }
        }
        private void grid_enter(object sender, PointerRoutedEventArgs e)
        {
            this.GRoot.Background = new SolidColorBrush(Color.FromArgb(255, 255, 255, 255));
        }

        private void Grid_exited(object sender, PointerRoutedEventArgs e)
        {
            this.GRoot.Background = new SolidColorBrush(Color.FromArgb(0, 0, 0, 0));
        }
    }
}
